home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / perl5 / Glib / ParseXSDoc.pm < prev    next >
Encoding:
Perl POD Document  |  2008-03-30  |  22.4 KB  |  869 lines

  1. package Glib::ParseXSDoc;
  2.  
  3. # vim: set ts=4 :
  4.  
  5. use strict;
  6. use Data::Dumper;
  7. use Storable qw(store_fd);
  8. use Exporter;
  9. use Carp;
  10.  
  11. our @ISA = qw(Exporter);
  12. our @EXPORT = qw(
  13.     xsdocparse
  14. );
  15.  
  16. our $VERSION = '1.003';
  17.  
  18. our $NOISY = $ENV{NOISYDOC};
  19.  
  20. =head1 NAME
  21.  
  22. Glib::ParseXSDoc - Parse POD and XSub declarations from XS files.
  23.  
  24. =head1 DESCRIPTION
  25.  
  26. This is the heart of an automatic API reference documentation system for
  27. XS-based Perl modules.  FIXME more info here!!
  28.  
  29. FIXME document recognized POD directives and the output data structures
  30.  
  31. =head1 FUNCTIONS
  32.  
  33. =over
  34.  
  35. =item xsdocparse (@filenames)
  36.  
  37. Parse xs files for xsub signatures and pod.  Writes to standard output a
  38. data structure suitable for eval'ing in another Perl script, describing
  39. all the stuff found.  The output contains three variables:
  40.  
  41. =over
  42.  
  43. =item $xspods = ARRAYREF
  44.  
  45. array of pods found in the verbatim C portion of the XS file, listed in the
  46. order found.  These are assumed to pertain to the XS/C api, not the Perl api.
  47. Any C<=for apidoc> paragraphs following an C<=object> paragraphs in the
  48. verbatim sections are stripped (as are the C<=object> paragraphs), and will
  49. appear instead in C<< $data->{$package}{pods} >>.
  50.  
  51. =item $data = HASHREF
  52.  
  53. big hash keyed by package name (as found in the MODULE line), containing under
  54. each key a hash with all the xsubs and pods in that package, in the order
  55. found.  Packages are consolidated across multiple files.
  56.  
  57. =back
  58.  
  59. FYI, this creates a new parser and calls C<parse_file> on it for each
  60. input filename; then calls C<swizzle_pods> to ensure that any
  61. C<=for apidoc name> pods are matched up with their target xsubs; and
  62. finally calls Data::Dumper to write the data to stdout.  So, if you want
  63. to get finer control over how the output is created, or keep all the data
  64. in-process, now you know how.  :-)
  65.  
  66. =cut
  67.  
  68. sub xsdocparse {
  69.     my @filenames = @_;
  70.  
  71.     my $parser = Glib::ParseXSDoc->new;
  72.     foreach my $filename (@filenames) {
  73.         $parser->parse_file ($filename);
  74.     }
  75.     $parser->canonicalize_xsubs;
  76.     $parser->swizzle_pods;
  77.     $parser->preprocess_pods;
  78.     $parser->clean_out_empty_pods;
  79.  
  80.     print "# THIS FILE IS AUTOMATICALLY GENERATED - ANY CHANGES WILL BE LOST\n";
  81.     print "# generated by $0 ".scalar (localtime)."\n";
  82.     print "# input files:\n";
  83.     map { print "#   $_\n" } @filenames;
  84.     print "#\n\n";
  85.  
  86.     # Data::Dumper converts the whole output to a string, and consequently
  87.     # uses an obscene amount of ram on Gtk2's nearly 200 xs files.  Use
  88.     # Storable unless the user really really wants to force us to fall
  89.     # back to Data::Dumper.
  90.     if ($ENV{FORCE_DATA_DUMPER}) {
  91.         $Data::Dumper::Purity = 1;
  92.         print Data::Dumper->Dump([$parser->{xspods}, $parser->{data}],
  93.                                [qw($xspods            $data)]);
  94.         print "\n1;\n";
  95.     } else {
  96.         print "use Storable qw(fd_retrieve);\n";
  97.         print "\$xspods = fd_retrieve \\*DATA;\n";
  98.         print "\$data = fd_retrieve \\*DATA;\n";
  99.  
  100.         print "\n1;\n";
  101.         print "__DATA__\n";
  102.  
  103.         # NOTE: don't assume STDOUT, because other code may have select'd
  104.         # a different file handle.
  105.         store_fd $parser->{xspods}, select;
  106.         store_fd $parser->{data}, select;
  107.     }
  108.  
  109.     return [ keys %{$parser->{data}} ];
  110. }
  111.  
  112.  
  113. =back
  114.  
  115. =cut
  116.  
  117. # =========================================================================
  118.  
  119. =head1 METHODS
  120.  
  121. =over
  122.  
  123. =item $Glib::ParseXSDoc::verbose
  124.  
  125. If true, this causes the parser to be verbose.
  126.  
  127. =cut
  128.  
  129. our $verbose = undef;
  130.  
  131.  
  132. =item $parser = Glib::ParseXSDoc->new
  133.  
  134. Create a new xsub parser.
  135.  
  136. =cut
  137.  
  138. sub new {
  139.     my $class = shift;
  140.     return bless {
  141.         # state
  142.         module => undef,
  143.         package => undef,
  144.         prefix => undef,
  145.         # data
  146.         xspods => [],    #pods for the exported xs interface, e.g. the C stuff
  147.         data => {},    # all the shizzle, by package name
  148.     }, $class;
  149. }
  150.  
  151. =item string = $parser->package
  152.  
  153. Get the current package name.  Falls back to the module name.  Will be undef
  154. if the parser hasn't reached the first MODULE line.
  155.  
  156. =cut
  157.  
  158. sub package {
  159.         my $self = shift;
  160.         return ($self->{package} || $self->{module})
  161. }
  162.  
  163. =item HASHREF = $parser->pkgdata
  164.  
  165. The data hash corresponding to the current package, honoring the most recently
  166. encountered C<=for object> directive.  Ensures that it exists.
  167. Returns a reference to the member of the main data structure, so modifications
  168. are permanent and useful.
  169.  
  170. =cut
  171.  
  172. sub pkgdata {
  173.         my $self = shift;
  174.         my $pkg = $self->{object} || $self->package;
  175.         my $pkgdata = $self->{data}{$pkg};
  176.         if (not defined $pkgdata) {
  177.                 $pkgdata = {};
  178.                 $self->{data}{$pkg} = $pkgdata;
  179.         }
  180.         return $pkgdata;
  181. }
  182.  
  183.  
  184. =item $parser->parse_file (filename)
  185.  
  186. Parse one xs file.  Stores all the collected data in I<$parser>'s internal
  187. data structures.
  188.  
  189. =cut
  190.  
  191. sub parse_file {
  192.     my $self = shift;
  193.     my $filename = shift;
  194.  
  195.     local *IN;
  196.     open IN, $filename or die "can't open $filename: $!\n";
  197.     print STDERR "scanning $filename\n" if $verbose;
  198.     $self->{filehandle} = \*IN;
  199.     $self->{filename} = $filename;
  200.  
  201.     # there was once a single state machine to parse an entire
  202.     # file, but it turned into a bi-level state machine because
  203.     # of the two-part nature of XS files.  that's silly, so i've
  204.     # broken it into two loops: the part that scans up to the
  205.     # first MODULE line, and the part that scans the rest of the
  206.     # file.
  207.  
  208.     my $lastpod = undef;    # most recently-read pod (for next xsub)
  209.     my @thesepackages = ();    # packages seen in this file
  210.  
  211.     # In the verbatim C portion of the file:
  212.     # seek the first MODULE line *outside* comments.
  213.     # collect any pod we encounter; only certain ones are 
  214.     # precious to us...  my... preciousssss... ahem.
  215.     $self->{module}  = undef;
  216.     $self->{package} = undef;
  217.     $self->{prefix}  = undef;
  218.     $self->{object}  = undef;
  219.     while (<IN>) {
  220.         chomp;
  221.         # in the verbatim C section before the first MODULE line,
  222.         # we need to be on the lookout for a few things...
  223.         # we need the first MODULE line, of course...
  224.         if ($self->is_module_line ($_)) {
  225.             last; # go to the next state machine.
  226.  
  227.         # mostly we want pods.
  228.         } elsif (/^=/) {
  229.             my $thispod = $self->slurp_pod_paragraph ($_);
  230.             # we're only interested in certain pod directives here.
  231.             if (/^=for\s+(apidoc|object)\b/) {
  232.                 my $which = $1;
  233.                 warn "$filename:".($.-@{$thispod->{lines}}+1).":"
  234.                    . " =for $which found before "
  235.                    . "MODULE directive\n";
  236.             }
  237.             push @{ $self->{xspods} }, $thispod;
  238.  
  239. ##        # we also need to track whether we're in a C comment, because
  240. ##        # MODULE directives are ignore in multiline comments.
  241. ##        } elsif (m{/\*}) {
  242. ##            # there was an open comment marker on this line.
  243. ##            # see if it's alone.
  244. ##            s{/\*.*\*/}{}g;
  245. ##            if (m{/\*}) {
  246. ##                # look for the end...
  247. ##                while (<IN>) {
  248. ##                }
  249. ##            }
  250.         }
  251.     }
  252.  
  253.     # preprocessor conditionals
  254.     my @cond;
  255.  
  256.     $lastpod = undef;
  257.     while (<IN>) {
  258.         #
  259.         # we're seeking xsubs and pods to document the Perl interface.
  260.         #
  261.         if ($self->is_module_line ($_)) {
  262.             # xsubs cannot steal pods across MODULE lines.
  263.             $lastpod = undef;
  264.  
  265.         } elsif (/^\s*$/) {
  266.             # ignore blank lines; but a blank line after a pod
  267.             # means it can't be associated with an xsub.
  268.             $lastpod = undef;
  269.  
  270.         } elsif (/^\s*#\s*(if|ifdef|ifndef)\s*(\s.*)$/) {
  271.             #warn "conditional $1 $2\n";
  272.             push @cond, $2;
  273.             #print Dumper(\@cond);
  274.         } elsif (/^\s*#\s*else\s*(\s.*)?$/) {
  275.             #warn "else $cond[-1]\n";
  276.             if (exists $cond[$#cond]) {
  277.                 $cond[$#cond] = '!' . $cond[$#cond];
  278.             }
  279.         } elsif (/^\s*#\s*endif\s*(\s.*)?$/) {
  280.             #warn "endif $cond[-1]\n";
  281.             pop @cond;
  282.         } elsif (/^\s*#/) {
  283.             # ignore comments.  we've already determined that 
  284.             # this isn't a preprocessor directive (or at least
  285.             # not one in which we're interested).
  286.  
  287.         } elsif (/^(BOOT|PROTOTYPES)/) {
  288.             # ignore keyword lines in which we aren't interested
  289.  
  290.         } elsif (/^=/) {
  291.             # slurp in pod, up to and including the next =cut.
  292.             # put it in $lastpod so that the next-discovered
  293.             # xsub can claim it.
  294.             $lastpod = $self->slurp_pod_paragraph ($_);
  295.  
  296.             # we're interested in certain pod directives at
  297.             # this point...
  298.             if (/^=for\s+object(?:\s+([\w\:]*))?(.*)/) {
  299.                 $self->{object} = $1;
  300.                 if ($2) {
  301.                     $self->pkgdata->{blurb} = $2;
  302.                     $self->pkgdata->{blurb} =~ s/^\s*-\s*//;
  303.  
  304.                     # If the line has the special form
  305.                     # "=for object Foo (Bar)", we take this
  306.                     # to mean: document the object Bar in
  307.                     # the file Foo.
  308.                     if ($self->pkgdata->{blurb} =~ s/\s*\((.*)\)//)
  309.                     {
  310.                         print STDERR "Documenting object $1 in file "
  311.                                     .$self->{object}."\n";
  312.                         $self->pkgdata->{object} = $1;
  313.                         if ('' eq $self->pkgdata->{blurb})
  314.                         {
  315.                             delete $self->pkgdata->{blurb};
  316.                         }
  317.                     }
  318.                 }
  319.             } elsif (/^=for\s+(enum|flags)\s+([\w:]+)/) {
  320.                 push @{ $self->pkgdata->{enums} }, {
  321.                     type => $1,
  322.                     name => $2,
  323.                     pod => $lastpod,
  324.                 };
  325.                 # claim this pod now!
  326.                 $lastpod = undef;
  327.             } elsif (/^=for\s+see_also\s+(.+)$/) {
  328.                 push @{ $self->pkgdata->{see_alsos} }, $1;
  329.                 # claim this pod now!
  330.                 $lastpod = undef;
  331.             } elsif (/^=for\s+deprecated_by\s+([\w:]+)$/) {
  332.                 push @{ $self->pkgdata->{deprecated_bys} }, $1;
  333.                 $lastpod = undef;
  334.             }
  335.             push @{ $self->pkgdata->{pods} }, $lastpod
  336.                 if defined $lastpod;
  337.  
  338.         } elsif (/^\w+/) {
  339.             # there's something at the beginning of the line!
  340.             # we've ruled out everything else, so this must be
  341.             # an xsub.  slurp in everything up to the next
  342.             # blank line (or end of file).   i know that's not
  343.             # *really* an entire XSUB body, but we don't care
  344.             # -- we only need the return value, name, arg types,
  345.             # and body type, and there aren't supposed to be 
  346.             # blank lines in all of that.
  347.             my @thisxsub = ($_);
  348.             while (<IN>) {
  349.                 chomp;
  350.                 last if /^\s*$/;
  351.                 push @thisxsub, $_;
  352.             }
  353.             my $xsub = $self->parse_xsub (\@thisxsub);
  354.             if ($lastpod) {
  355.                 # aha! we'll lay claim to that...
  356.                 pop @{ $self->pkgdata->{pods} };
  357.                 $xsub->{pod} = $lastpod;
  358.                 $lastpod = undef;
  359.             }
  360.             $xsub->{preprocessor_conditionals} = [ @cond ];
  361.             push @{ $self->pkgdata->{xsubs} }, $xsub;
  362.  
  363.         } else {
  364.             # this is probably xsub function body, comment, or
  365.             # some other stuff we don't care about.
  366.         }
  367.     }
  368.  
  369.     # that's it for this file...
  370.     close IN;
  371.     delete $self->{filehandle};
  372.     delete $self->{filename};
  373. }
  374.  
  375.  
  376. =item $parser->swizzle_pods
  377.  
  378. Match C<=for apidoc> pods to xsubs.
  379.  
  380. =cut
  381.  
  382. sub swizzle_pods {
  383.     my $self = shift;
  384.     foreach my $package (keys %{$self->{data}}) {
  385.         my $pkgdata = $self->{data}{$package};
  386.         next unless $pkgdata->{pods};
  387.         next unless $pkgdata->{xsubs};
  388.         my $pods = $pkgdata->{pods};
  389.         for (my $i = @$pods-1 ; $i >= 0 ; $i--) {
  390.             my $firstline = $pods->[$i]{lines}[0];
  391.             next unless $firstline =~ /=for\s+apidoc\s+([:\w]+)\s*/;
  392.             my $name = $1;
  393.             foreach my $xsub (@{ $pkgdata->{xsubs} }) {
  394.                 if ($name eq $xsub->{symname}) {
  395.                     $xsub->{pod} = $pods->[$i];
  396.                     splice @$pods, $i, 1;
  397.                     last;
  398.                 }
  399.             }
  400.         }
  401.     }
  402. }
  403.  
  404.  
  405. =item $parser->preprocess_pods
  406.  
  407. Honor the C<__hide__> and C<__function__> directives in C<=for apidoc> lines.
  408.  
  409. We look for the strings anywhere, but you'll typically have it at the end of
  410. the line, e.g.:
  411.  
  412.   =for apidoc symname __hide__        for detached blocks
  413.   =for apidoc __hide__                for attached blocks
  414.  
  415.   =for apidoc symname __function__    for functions rather than methods
  416.   =for apidoc __function__            for functions rather than methods
  417.  
  418. =cut
  419.  
  420. sub preprocess_pods {
  421.     my $self = shift;
  422.     foreach my $package (keys %{$self->{data}}) {
  423.         my $pkgdata = $self->{data}{$package};
  424.  
  425.         foreach (@{$pkgdata->{pods}})
  426.         {
  427.             my $firstline = $_->{lines}[0];
  428.             if ($firstline) {
  429.                 $_->{position} = $1 if ($firstline =~ /=for\s+position\s+(\w+)/);
  430.             }
  431.         }
  432.  
  433.         next unless $pkgdata->{xsubs};
  434.  
  435.         # look for magic keywords in the =for apidoc
  436.         foreach (@{$pkgdata->{xsubs}})
  437.         {
  438.             my $firstline = $_->{pod}{lines}[0];
  439.             if ($firstline) {
  440.                 $_->{function} = ($firstline =~ /__function__/);
  441.                 $_->{hidden} = ($firstline =~ /__hide__/);
  442.                 $_->{deprecated} = ($firstline =~ /__deprecated__/);
  443.                 $_->{gerror} = ($firstline =~ /__gerror__/);
  444.             }
  445.         }
  446.     }
  447. }
  448.  
  449.  
  450. # ===============================================================
  451.  
  452. =item bool = $parser->is_module_line ($line)
  453.  
  454. Analyze I<$line> to see if it contains an XS MODULE directive.  If so, returns
  455. true after setting the I<$parser>'s I<module>, I<package>, and I<prefix>
  456. accordingly.
  457.  
  458. =cut
  459.  
  460. sub is_module_line {
  461.     my $self = shift;
  462.     my $l = shift;
  463.     if ($l =~ /^MODULE\s*=\s*([:\w]+)
  464.                 (?:\s+PACKAGE\s*=\s*([:\w]+)
  465.                 (?:\s+PREFIX\s*=\s*([:\w]+))?)?
  466.                 /x) {
  467.         $self->{module}  = $1;
  468.         $self->{package} = $2 || $self->{module};
  469.         $self->{prefix}  = $3;
  470.         $self->{object}  = undef;
  471.         return 1;
  472.     } else {
  473.         return 0;
  474.     }
  475. }
  476.  
  477.  
  478. =item $pod = $parser->slurp_pod_paragraph ($firstline, $term_regex=/^=cut\s*/)
  479.  
  480. Slurp up POD lines from I<$filehandle> from here to the next
  481. I<$term_regex> or EOF.  Since you probably already read a
  482. line to determine that we needed to start a pod, you can pass
  483. that first line to be included.
  484.  
  485. =cut
  486.  
  487. sub slurp_pod_paragraph {
  488.     my $parser     = shift;
  489.     my $firstline  = shift;
  490.     my $term_regex = shift || qr/^=cut\s*/o;
  491.     my $filehandle = $parser->{filehandle};
  492.  
  493.     # just in case.
  494.     chomp $firstline;
  495.  
  496.     my @lines = $firstline ? ($firstline) : ();
  497.     while (my $line = <$filehandle>) {
  498.         chomp $line;
  499.         push @lines, $line;
  500.         last if $line =~ m/$term_regex/;
  501.     }
  502.  
  503.     return {
  504.         filename => $parser->{filename},
  505.         line => $. - @lines,
  506.         lines => \@lines,
  507.     };
  508. }
  509.  
  510.  
  511. =item $xsub = $parser->parse_xsub (\@lines)
  512.  
  513. =item $xsub = $parser->parse_xsub (@lines)
  514.  
  515. Parse an xsub header, in the form of a list of lines,
  516. into a data structure describing the xsub.  That includes
  517. pulling out the argument types, aliases, and code type.
  518.  
  519. Without artificial intelligence, we cannot reliably 
  520. determine anything about the types or number of parameters
  521. returned from xsubs with PPCODE bodies.
  522.  
  523. OUTLIST parameters are pulled from the args list and put
  524. into an "outlist" key.  IN_OUTLIST parameters are put into
  525. both.
  526.  
  527. Data type names are not mangled at all.
  528.  
  529. Note that the method can take either a list of lines or a reference to a
  530. list of lines.  The flat list form is provided for compatibility; the
  531. reference form is preferred, to avoid duplicating a potentially large list
  532. of strings.
  533.  
  534. =cut
  535.  
  536. sub parse_xsub {
  537.     my ($self, @thisxsub) = @_;
  538.  
  539.     # allow for pass-by-reference.
  540.     @thisxsub = @{ $thisxsub[0] }
  541.         if @thisxsub == 1 && 'ARRAY' eq ref $thisxsub[0];
  542.  
  543.     map { s/#.*$// } @thisxsub;
  544.  
  545.     my $filename = $self->{filename};
  546.     my $oldwarn = $SIG{__WARN__};
  547. #$SIG{__WARN__} = sub {
  548. #        warn "$self->{filename}:$.:  "
  549. #           . join(" / ", $self->{module}||"", $self->{package}||"")
  550. #           . "\n    $_[0]\n   ".Dumper(\@thisxsub)
  551. #};
  552.  
  553.     my $lineno = $. - @thisxsub;
  554.     my %xsub = (
  555.         'filename' => $filename,
  556.         'line' => ($.-@thisxsub),
  557.         'module' => $self->{module},
  558.         'package' => $self->package, # to be overwritten as needed
  559.     );
  560.     my $args;
  561.  
  562.     #warn Dumper(\@thisxsub);
  563.  
  564.     # merge continuation lines.  xsubpp allows continuation lines in the
  565.     # xsub arguments list and barfs on them in other spots, but with xsubpp
  566.     # providing such validation, we'll just cheat and merge any that we find.
  567.     # this will bork the line counting logic we have below, but i don't see
  568.     # a fix for it without major tearup of the code here.
  569.     my @foo = @thisxsub;
  570.     @thisxsub = shift @foo;
  571.     while (my $s = shift @foo) {
  572.         if ($thisxsub[$#thisxsub] =~ s/\\$//) {
  573.             chomp $thisxsub[$#thisxsub];
  574.             $thisxsub[$#thisxsub] .= $s;
  575.         } else {
  576.             push @thisxsub, $s;
  577.         }
  578.     }
  579.  
  580.     if ($thisxsub[0] =~ /^([^(]+\s+\*?)   # return type, possibly with a *
  581.                           \b([:\w]+)\s*   # symbol name
  582.                           \(              # open paren
  583.                             (.*)          # whatever's inside, if anything
  584.                           \)              # close paren, maybe with space
  585.                           \s*;?\s*$/x) {  # and maybe other junk at the end
  586.         # all on one line
  587.         $xsub{symname} = $2;
  588.         $args = $3;
  589.         my $r = $1;
  590.         $xsub{return_type} = [$r]
  591.             unless $r =~ /^void\s*$/;
  592.         shift @thisxsub; $lineno++;
  593.  
  594.     } elsif ($thisxsub[1] =~ /^(\S+)\s*\((.*)\);?\s*$/) {
  595.         # multiple lines
  596.         $xsub{symname} = $1;
  597.         $args = $2;
  598.         # return type is on line 0
  599.         $thisxsub[0] =~ s/\s*$//;
  600.         $xsub{return_type} = [$thisxsub[0]]
  601.             unless $thisxsub[0] =~ /^void\s*$/;
  602.         shift @thisxsub; $lineno++;
  603.         shift @thisxsub; $lineno++;
  604.     }
  605.  
  606.     # eat padding spaces from the arg string.  i tried several ways of
  607.     # building this into the regexen above, but found nothing that still
  608.     # allowed the arg string to be empty, which we'll have for functions
  609.     # (not methods) without resorting to extremely arcane negatory
  610.     # lookbeside assertiveness operators.
  611.     $args =~ s/^\s*//;
  612.     $args =~ s/\s*$//;
  613.  
  614.     # we can get empty arg strings on non-methods.
  615.     #warn "$filename:$lineno: WTF : args string is empty\n"
  616.     #    if not defined $args;
  617.  
  618.     my %args = ();
  619.     my @argstr = split /\s*,\s*/, $args;
  620.     #warn Dumper([$args, \%args, \@argstr]);
  621.     for (my $i = 0 ; $i < @argstr ; $i++) {
  622.         # the last one can be an ellipsis, let's handle that specially
  623.         if ($i == $#argstr and $argstr[$i] eq '...') {
  624.             $args{'...'} = { name => '...', };
  625.             push @{ $xsub{args} }, $args{'...'};
  626.             last;
  627.         }
  628.         if ($argstr[$i] =~
  629.                        /^(?:(IN_OUTLIST|OUTLIST)\s+)? # OUTLIST would be 1st
  630.                          ([^=]+(?:\b|\s))?  # arg type is optional, too
  631.                          (\w+)              # arg name
  632.                          (?:\s*=\s*(.+))?   # possibly a default value
  633.                          $/x) {
  634.             if (defined $1) {
  635.                 push @{ $xsub{outlist} }, {
  636.                     type => $2,
  637.                     name => $3,
  638.                 };
  639.                 if ($1 eq 'IN_OUTLIST') {
  640.                     # also an arg
  641.                     $args{$3} = {
  642.                         type => $2,
  643.                         name => $3,
  644.                     };
  645.                     $args{$3}{default} = $4 if defined $4;
  646.                     push @{ $xsub{args} }, $args{$3};
  647.                 }
  648.             
  649.             } else {
  650.                 $args{$3} = {
  651.                     type => $2,
  652.                     name => $3,
  653.                 };
  654.                 $args{$3}{default} = $4 if defined $4;
  655.                 push @{ $xsub{args} }, $args{$3};
  656.             }
  657.         } elsif ($argstr[$i] =~ /^g?int\s+length\((\w+)\)$/) {
  658.             #warn " ******* $i is string length of $1 *****\n";
  659.         } else {
  660.             warn "$filename:$lineno: ($xsub{symname}) don't know how to"
  661.                . " parse arg $i, '$argstr[$i]'\n";
  662.         }
  663.     }
  664.  
  665.     
  666.  
  667.     my $xstate = 'args';
  668.     while ($_ = shift @thisxsub) {
  669.         if (/^\s*ALIAS:/) {
  670.             $xstate = 'alias';
  671.         } elsif (/\s*(PREINIT|CLEANUP|OUTPUT|C_ARGS):/) {
  672.             $xstate = 'code';
  673.         } elsif (/\s*(PPCODE|CODE):/) {
  674.             $xsub{codetype} = $1;
  675.             last;
  676.         } elsif ($xstate eq 'alias') {
  677.             /^\s*([:\w]+)\s*=\s*(\d+)\s*$/;
  678.             if (defined $2) {
  679.                 $xsub{alias}{$1} = $2;
  680.             } else {
  681.                 warn "$filename:$lineno: WTF : seeking alias on line $_\n";
  682.             }
  683.         } elsif ($xstate eq 'args') {
  684.             if (/^\s*
  685.                   (.+(?:\b|\s))      # datatype
  686.                   (\w+)              # arg name
  687.                   ;?                 # optional trailing semicolon
  688.                   \s*$/x)
  689.             {
  690.                 if (exists $args{$2}) {
  691.                     $args{$2}{type} = $1
  692.                 } else {
  693.                     warn "$filename:$lineno: unused arg $2\n";
  694.                     warn "  line was '$_'\n";
  695.                 }
  696.             } elsif (/^\s*/) {
  697.                 # must've stripped a comment.
  698.             } else {
  699.                 warn "$filename:$lineno: WTF : seeking args on line $_\n";
  700.             }
  701.         }
  702.         $lineno++;
  703.     }
  704.  
  705.     # mangle the symbol name from an xsub into its actual perl name.
  706.     $xsub{original_name} = $xsub{symname};
  707.     if (defined $self->{prefix}) {
  708.         my $pkg = $self->package;
  709.         $xsub{symname} =~ s/^($self->{prefix})?/$pkg\::/;
  710.     } else {
  711.         $xsub{symname} = ($self->package)."::".$xsub{symname};
  712.     }
  713.  
  714.     # sanitize all the C type declarations, which we have 
  715.     # collected in the arguments, outlist, and return types.
  716.     if ($xsub{args}) {
  717.         foreach my $a (@{ $xsub{args} }) {
  718.             $a->{type} = sanitize_type ($a->{type})
  719.                 if defined $a->{type};
  720.         }
  721.     }
  722.     if ($xsub{outlist}) {
  723.         foreach my $a (@{ $xsub{outlist} }) {
  724.             $a->{type} = sanitize_type ($a->{type})
  725.                 if defined $a->{type};
  726.         }
  727.     }
  728.     if ($xsub{return_type}) {
  729.         for (my $i = 0 ; $i < @{ $xsub{return_type} } ; $i++) {
  730.             $xsub{return_type}[$i] =
  731.                 sanitize_type ($xsub{return_type}[$i]);
  732.         }
  733.     }
  734.  
  735.     $SIG{__WARN__} = $oldwarn;
  736.  
  737.     return \%xsub;
  738. }
  739.  
  740.  
  741.  
  742. sub sanitize_type {
  743.         local $_ = shift;
  744.         s/\s+/ /g;        # squash all whitespace
  745.         s/^\s//;          # zap leading space
  746.         s/\s$//;          # zap trailing space
  747.         s/(?<=\S)\*$/ */; # stars may not be glued to the name
  748.         return $_;
  749. }
  750.  
  751.  
  752. sub canonicalize_xsubs {
  753.     my $self = shift;
  754.  
  755.     return undef unless 'HASH' eq ref $self->{data};
  756.  
  757.     # make sure that each package contains an xsub hash for each
  758.     # xsub, whether an alias or not.
  759.     foreach my $package (keys %{$self->{data}}) {
  760.         my $pkgdata = $self->{data}{$package};
  761.         next unless $pkgdata or $pkgdata->{xsubs};
  762.         my $xsubs = $pkgdata->{xsubs};
  763.         @$xsubs = map { split_aliases ($_) } @$xsubs;
  764.     }
  765. }
  766.  
  767. sub split_aliases {
  768.     my $xsub = shift;
  769.     return $xsub unless exists $xsub->{alias};
  770.     return $xsub unless 'HASH' eq ref $xsub->{alias};
  771.     my %aliases = %{ $xsub->{alias} };
  772.     my @xsubs = ();
  773.     my %seen = ();
  774.     foreach my $a (sort { $aliases{$a} <=> $aliases{$b} } keys %aliases) {
  775.         push @xsubs, {
  776.             %$xsub,
  777.             symname => $a,
  778.             pod => undef,
  779.             # we do a deep copy on the args, so that changes to one do not
  780.             # affect another.  in particular, adding docs or hiding an arg
  781.             # in one xsub shouldn't affect another.
  782.             args => deep_copy_ref ($xsub->{args}),
  783.         };
  784.         $seen{ $aliases{$a} }++;
  785.     }
  786.     if (! $seen{0}) {
  787.         unshift @xsubs, $xsub;
  788.     }
  789.  
  790.     return @xsubs;
  791. }
  792.  
  793.  
  794. sub deep_copy_ref {
  795.         my $ref = shift;
  796.         return undef if not $ref;
  797.         my $reftype = ref $ref;
  798.         if ('ARRAY' eq $reftype) {
  799.                 my @newary = map { deep_copy_ref ($_) } @$ref;
  800.                 return \@newary;
  801.         } elsif ('HASH' eq $reftype) {
  802.                 my %newhash = map { $_, deep_copy_ref ($ref->{$_}) } keys %$ref;
  803.                 return \%newhash;
  804.         } else {
  805.                 return $ref;
  806.         }
  807. }
  808.  
  809. =item $parser->clean_out_empty_pods
  810.  
  811. Looks through the data member of the parser and removes any keys (and
  812. associated values) when no pod, enums, and xsubs exist for the package.
  813.  
  814. =cut
  815.  
  816. sub clean_out_empty_pods
  817. {
  818.     my $data = shift;
  819.     return unless (exists ($data->{data}));
  820.     $data = $data->{data};
  821.  
  822.     my $pod;
  823.     my $xsub;
  824.     foreach (keys %$data)    
  825.     {
  826.         $pod = $data->{$_};
  827.         next if ((exists $pod->{pods} and scalar @{$pod->{pods}}) or
  828.                  exists $pod->{enums} or 
  829.                  scalar (grep (!/DESTROY/, 
  830.                                  map { $_->{hidden} 
  831.                                        ? ()
  832.                                        : $_->{symname} }
  833.                                      @{$pod->{xsubs}})));
  834.         #print STDERR "Deleting $_ from doc.pl's \$data\n";
  835.         delete $data->{$_}; 
  836.     }
  837. }
  838.  
  839.  
  840. 1;
  841.  
  842. __END__
  843.  
  844. =back
  845.  
  846. =head1 AUTHOR
  847.  
  848. muppet E<lt>scott at asofyet dot orgE<gt>
  849.  
  850. =head1 COPYRIGHT AND LICENSE
  851.  
  852. Copyright (C) 2003, 2004 by muppet
  853.  
  854. This library is free software; you can redistribute it and/or modify it under
  855. the terms of the GNU Library General Public License as published by the Free
  856. Software Foundation; either version 2.1 of the License, or (at your option) any
  857. later version.
  858.  
  859. This library is distributed in the hope that it will be useful, but WITHOUT ANY
  860. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  861. PARTICULAR PURPOSE.  See the GNU Library General Public License for more
  862. details.
  863.  
  864. You should have received a copy of the GNU Library General Public License along
  865. with this library; if not, write to the Free Software Foundation, Inc., 59
  866. Temple Place - Suite 330, Boston, MA  02111-1307  USA.
  867.  
  868. =cut
  869.